package com.spice.service.controller;

import com.spice.common.result.Response;
import com.spice.service.entity.job.JobExecutor;
import com.spice.service.util.MultipartFileUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Slf4j
@RestController
@RequestMapping("/api/utils")
public class UtilsController {

//    @ApiOperation(value = "获取文件内容(byte数组)")
//    @GetMapping(value = "/getFileContent/{fileName}", produces = MediaType.ALL_VALUE)
//    public byte[] getFile(@PathVariable("fileName") String fileName) throws IOException {
//        return MultipartFileUtil.getFile(fileName);
//
//    }

    @ApiOperation(value = "下载文件内容(byte数组)")
    @GetMapping(value = "/downloadFile/{fileName}")
    public Response downloadFile(@PathVariable("fileName") String fileName) throws IOException {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        // 设置编码
        String hehe = new String(fileName.getBytes("utf-8"), "iso-8859-1");
        // 设置扩展头，当Content-Type 的类型为要下载的类型时 , 这个信息头会告诉浏览器这个文件的名字和类型。
        response.setHeader("Content-Disposition", "attachment;filename=" + hehe);
        MultipartFileUtil.download(fileName, response);
        return Response.ok();
    }


    @ApiOperation(value = "获取图片")
    @GetMapping(value = "/getImage/{fileName}", produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] getPhoto(@PathVariable("fileName") String fileName) throws InterruptedException, IOException {
        return MultipartFileUtil.getFile(fileName);
    }


    @ApiOperation(value = "上传文件(不得大于128MB)")
    @PostMapping(value = "/uploadFile", produces = "application/json;charset=UTF-8")
    public Response uploadFile(MultipartFile file, HttpServletRequest request) {
        return Response.ok()
                .data("fileInfo", MultipartFileUtil
                        .saveFile(file).loadUrl(request));
    }


    @ApiOperation(value = "上传图片(不得大于128MB)")
    @PostMapping(value = "/uploadImage", produces = "application/json;charset=UTF-8")
    public Response uploadImage(MultipartFile file, HttpServletRequest request) {
        if (!MultipartFileUtil.isImage(file)) {
            return Response.error().message("该文件不是image/jpeg、image/png格式");
        }
        return Response.ok()
                .data("fileInfo", MultipartFileUtil
                        .saveImage(file).loadUrl(request));

    }

    @ApiOperation("获取目标工作信息")
    @PostMapping(value = "/getJob/{id}", produces = "application/json;charset=UTF-8")
    public Response test1(@PathVariable("id") String id) {
        return Response.ok().data("job", JobExecutor.searchDoneJob(id));
    }
}
